// Client

AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")

// Allowed

ENT.Allowed = {
	prop_physics = true,
	player = true
}

// Include

include('shared.lua')

// Initialize

function ENT:Initialize()
	self.Entity:PhysicsInit(SOLID_VPHYSICS)
	self.Entity:SetMoveType(MOVETYPE_VPHYSICS)
	self.Entity:SetSolid(SOLID_VPHYSICS)
	self.Entity:SetUseType(ONOFF_USE)
end

// Validation

function ENT:Validation(Entity)
	local Class = Entity:GetClass()
	
	if (self.Allowed[Class]) then
		return true
	end
	
	return false
end

// Check owner

function ENT:CheckOwner(Entity)
	for K, V in pairs(g_SBoxObjects) do
		for B, J in pairs(V) do
			for _, E in pairs(J) do
				if (E == Entity) then
					return K
				end
			end
		end
	end
	
	// Let's try cameras
	
	for K, V in pairs(GAMEMODE.CameraList) do
		for B, J in pairs(V) do
			if (J == Entity) then
				return K
			end
		end
	end
	
	// Return nil

	return nil
end

// Think

function ENT:Think()
	local Position = self.Entity:GetPos()
	local Radius   = self:GetEntityRadius()
	local Type     = self:GetEntityType()
	local Owner    = self:GetPlayer()
	local Current  = self.Entity:GetNetworkedEntity("Target")
	local Delay    = 0.05
	
	// Check if they are valid or a player
	
	local Valid = Current:IsValid()
	local Player = Current:IsPlayer()
	
	// Check if they are allive
	
	if (Player) then Valid = Current:Alive() end
	
	if not (Valid) then
		Current = false
	else
		local Ang = (Position - Current:GetPos()):Length()
		
		if (Ang > Radius) then Current = false end
	end
	
	// Check
	
	if not (Current) then
		if (self.Entity:GetNetworkedInt("Target") != 0) then self.Entity:SetNetworkedInt("Target", 0) end
	end
	
	// Sphere
	
	local Area = ents.FindInSphere(Position, Radius)
	
	for K, V in pairs(Area) do
		if (V != Owner) then
			if (self:Validation(V)) then
				if not (self:CheckOwner(V) == Owner:UniqueID()) then
					local Smaller = false
					
					if (Current) then
						local Entity = (Position - Current:GetPos()):Length()
						
						local Other = (Position - V:GetPos()):Length()
						
						Smaller = (Other < Entity)
					end
					
					// Check if it is closer
					
					if not (Current) or (Current == V) or (Smaller) then
						local Ang = V:GetPos()
						
						local Pl = V:IsPlayer() if (Pl) then Ang = Ang + Vector(0, 0, 32) end
						
						Ang = Ang - Position
						
						Ang = Ang:Normalize()
						
						local Trace = {}
						
						Trace.start = Position
						Trace.endpos = Position + (Ang * Radius)
						Trace.filter = self.Entity
						
						local TR = util.TraceLine(Trace)
					 
						if (TR.Entity and TR.Entity == V) then
							Delay = PerimeterTurret.List[Type](self:GetPlayer(), self.Entity, TR.Entity, Position, Ang)
							
							if (self.Entity:GetNetworkedEntity("Target") != V) then self.Entity:SetNetworkedEntity("Target", V) end
							if (self.Entity:GetNetworkedInt("Target") != 1) thenself.Entity:SetNetworkedInt("Target", 1) end
						end
					end
				end
			end
		end
	end
	
	// Set the next think
	
	self.Entity:NextThink(CurTime() + Delay)
	
	return true
end